package org.mybatis.generator.my.plugins;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.my.bean.Clazz;
import org.mybatis.generator.my.bean.ClazzField;

import java.util.List;
import java.util.Optional;

public class CollectModelDataPlugin extends PluginAdapter {

    private static Clazz clazz;

    public CollectModelDataPlugin() {
        CollectModelDataPlugin.clazz = new Clazz();
    }

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        CollectModelDataPlugin.clazz.setClassComment(Optional.ofNullable(introspectedTable.getRemarks()));
        CollectModelDataPlugin.clazz.setClassName(topLevelClass.getType().getShortName());
        CollectModelDataPlugin.clazz.setSupportName(StringUtils.uncapitalize(topLevelClass.getType().getShortName()));
        return true;
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        ClazzField clazzField = new ClazzField();
        clazzField.setFullFieldType(field.getType().getFullyQualifiedName())
                .setFieldType(field.getType().getShortName())
                .setFieldName(field.getName())
                .setColumnName(introspectedColumn.getActualColumnName())
                .setNullable(introspectedColumn.isNullable())
                .setFieldComment(Optional.ofNullable(introspectedColumn.getRemarks()));
        CollectModelDataPlugin.clazz.getFieldList().add(clazzField);
        return true;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        List<ClazzField> fieldList = CollectModelDataPlugin.clazz.getFieldList();
        for (ClazzField clazzField : fieldList) {
            if (introspectedColumn.getActualColumnName().equals(clazzField.getColumnName())) {
                clazzField.setGetMethodName(method.getName());
            }
        }
        return true;
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        List<ClazzField> fieldList = CollectModelDataPlugin.clazz.getFieldList();
        for (ClazzField clazzField : fieldList) {
            if (introspectedColumn.getActualColumnName().equals(clazzField.getColumnName())) {
                clazzField.setSetMethodName(method.getName());
            }
        }
        return true;
    }

    public static Clazz getClazz() {
        return CollectModelDataPlugin.clazz;
    }

}
